<?php
class Admin_Form_ValidateUser{
	
	// Thuoc tinh chua thong bao loi cua form
	protected $_messagesError = null;
	
	// Mang chua du lieu sau khi kiem tra
	protected $_arrData;
	
	public function __construct($arrParam = array(), $options = null){
		// Kiem tra user name
		if($arrParam['action'] == 'add'){
			$options = array('table'=>'user','field'=>'username');
		}else if($arrParam['action'] == 'edit'){
			$clause = 'userid != ' . $arrParam['id'];
			$options = array('table'=>'user','field'=>'username','exclude'=>$clause);
		}
		$validator = new Zend_Validate();		
		$validator->addValidator(new Zend_Validate_NotEmpty(), true)
				  ->addValidator(new Zend_Validate_StringLength(3,32), true)
				  ->addValidator(new Zend_Validate_Regex('#^[a-zA-Z0-9\-_\.\s]+$#'), true)
				  ->addValidator(new Zend_Validate_Db_NoRecordExists($options), true);
		if(!$validator->isValid($arrParam['username'])){
			$message = $validator->getMessages();
			$this->_messagesError['username'] = 'User name: ' . current($message);
			$arrParam['username'] = '';
		}
		
		// Kiem tra avatar
		$upload = new Zend_File_Transfer_Adapter_Http();
		$fileInfo = $upload->getFileInfo('avatar');
		$fileName = $fileInfo['avatar']['name'];
		
		if(!empty($fileName)){
			$upload->addValidator('Extension', true, array('jpg', 'gif', 'png', 'jpeg'), 'avatar');
			$upload->addValidator('Size', true, array('min'=>'2KB', 'max'=>'1000KB'));
			if(!$upload->isValid('avatar')){
				$message = $upload->getMessages();
				$this->_messagesError['avatar'] = 'Avatar: ' . current($message);
			}
		}
		
		// Kiem tra password
		$flag = false;
		if($arrParam['action'] == 'add'){
			$flag == true;
		}else if($arrParam['action'] == 'edit'){
			if(!empty($arrParam['password'])){
				$flag == true;
			}
		}
		if($flag == true){
			$validator = new Zend_Validate();
			$validator->addValidator(new Zend_Validate_NotEmpty(), true)
					  ->addValidator(new Zend_Validate_StringLength(8,32), true)
					  ->addValidator(new Zend_Validate_Regex('#^[a-zA-Z0-9@\#\$%\^$\*\-\+]+$#'), true);
			if(!$validator->isValid($arrParam['password'])){
				$message = $validator->getMessages();
				$this->_messagesError['password'] = 'Password: ' . current($message);
			}
		}
		
		//Kiem tra dia chi email
		if($arrParam['action'] == 'add'){
			$options = array('table'=>'user','field'=>'email');
		}else if($arrParam['action'] == 'edit'){
			$clause = 'userid != ' . $arrParam['id'];
			$options = array('table'=>'user','field'=>'email','exclude'=>$clause);
		}
		$validator = new Zend_Validate();
		$validator->addValidator(new Zend_Validate_NotEmpty(), true)
				  ->addValidator(new Zend_Validate_EmailAddress(), true)
				  ->addValidator(new Zend_Validate_Db_NoRecordExists($options), true);
		if(!$validator->isValid($arrParam['email'])){
			$message = $validator->getMessages();
			$this->_messagesError['email'] = 'Email: ' . current($message);
			$arrParam['email'] = '';
		}
		
		// Kiem tra group_id
		if($arrParam['group_id'] == 0){
			$this->_messagesError['group_id'] = 'Group: Please select a group';
		}
		
		// Kiem tra full name
		$validator = new Zend_Validate();
		$validator->addValidator(new Zend_Validate_NotEmpty(), true)
				  ->addValidator(new Zend_Validate_StringLength(8), true);
		if(!$validator->isValid($arrParam['fullname'])){
			$message = $validator->getMessages();
			$this->_messagesError['fullname'] = 'Full name: ' . current($message);
			$arrParam['fullname'] = '';
		}
		
		// Kiem tra birthday
		$validator = new Zend_Validate();
		$validator->addValidator(new Zend_Validate_NotEmpty(), true)
				  ->addValidator(new Zend_Validate_Date(array('format'=>'YYY-mm-dd')), true);
		if(!$validator->isValid($arrParam['birthday'])){
			$message = $validator->getMessages();
			$this->_messagesError['birthday'] = 'Birthday: ' . current($message);
			$arrParam['birthday'] = '';
		}
		
		// Kiem tra status
		if(empty($arrParam['status']) || !isset($arrParam['status'])){
			$arrParam['status'] = 0;
		}
		
		// Kiem tra sign
		$validator = new Zend_Validate();
		$validator->addValidator(new Zend_Validate_NotEmpty(), true)
				  ->addValidator(new Zend_Validate_StringLength(10), true);
		if(!$validator->isValid($arrParam['sign'])){
			$message = $validator->getMessages();
			$this->_messagesError['sign'] = 'Sign: ' . current($message);
			$arrParam['sign'] = '';
		}
		
		// Truyen cac gia tri dung vao mang arrData
		$this->_arrData = $arrParam;
		
	}
	
	// Kiem tra, neu co loi tra ve gia tri True
	public function isError(){
		if(count($this->_messagesError)>0){
			return true;
		}else{
			return false;
		}
	}
	
	// Tra ve mot mang cac thong bao loi
	public function getMessageError(){
		return $this->_messagesError;
	}
	
	// Tra ve mot mang du lieu sau khi kiem tra
	public function getData($options = null){
		if($options['upload'] == true){
			$this->_arrData['avatar'] = $this->uploadFile();
		}
		return $this->_arrData;
	}
	
	// Neu form co upload file
	// 1. upload tap tin
	// 2. resize tap tin (100x100 va 450x450)
	// 3. tra ve ten tap tin upload
	public function uploadFile(){
		// Duong dan den thu muc upload
		$upload_dir = FILES_PATH . '/users';
		
		// upload file
		$upload = new Australian_File_Upload();
		$fileInfo = $upload->getFileInfo('avatar');
		$fileName = $fileInfo['avatar']['name'];
		if(!empty($fileName)){
			$fileName = $upload->upload('avatar', $upload_dir . '/orignal', 
										array('task'=>'rename'), 'member_');
			
			$thumb = Australian_File_Images::create($upload_dir . '/orignal/' . $fileName);
			$thumb->resize(100,100)->save($upload_dir . '/img100x100/' . $fileName);
			
			$thumb = Australian_File_Images::create($upload_dir . '/orignal/' . $fileName);
			$thumb->resize(450,450)->save($upload_dir . '/img450x450/' . $fileName);
			
			if($this->_arrData['action'] == 'edit'){
				$upload->removeFile($upload_dir . '/orignal/' . $this->_arrData['current_avatar']);
				$upload->removeFile($upload_dir . '/img100x100/' . $this->_arrData['current_avatar']);
				$upload->removeFile($upload_dir . '/img450x450/' . $this->_arrData['current_avatar']);
			}
		}else{
			if($this->_arrData['action'] == 'edit'){
				$fileName = $this->_arrData['current_avatar'];
			}
		}
		
		return $fileName;
	}
}




